package com.basic.technology.study.thinkinjava.thread.threadshareresource;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author 程杰
 * @Date 2021/1/26 10:06
 * @Version 1.0
 */
public class EvenChecker implements Runnable {

    private IntGenerator generator;

    private final int id;

    public EvenChecker(IntGenerator g, int ident){
        this.generator = g;
        id = ident;
    }

    @Override
    public void run() {
        while (!generator.isCanceled()){
            int val = generator.next();
            if (val%2 != 0){
                System.out.println(val + " not even !");
                generator.cancel();
            }
        }
    }

    public static void test(IntGenerator gp, int count) {
        System.out.println(" Press Control-C to exit");

        ExecutorService exec = Executors.newCachedThreadPool();

        for (int i = 0; i < count; i++) {
            exec.execute(new EvenChecker(gp, count));
        }
    }

    public static void test(IntGenerator gp) {
        test(gp,10);
    }

}
